package com.clstu.dao_.testdao;

import com.clstu.dao_.dao.GoodsDAO;
import com.clstu.dao_.domain.Goods;
import org.testng.annotations.Test;

import java.util.List;

/**
 * 测试对GoodsDao的crud操作
 */
public class TestGoodsDao {
    @Test//编写方法测试GoodsDao的select操作
    public void testSelect(){
        GoodsDAO goodsDAO = new GoodsDAO();

        String sql = "select * from goods";
        List<Goods> goods = goodsDAO.queryMany(sql, Goods.class, null);
        System.out.println("多行查询================");
        for (Goods g : goods) {
            System.out.println(g);
        }

        sql = "select * from goods where id = ?";
        Goods goods1 = goodsDAO.querySingle(sql, Goods.class, 4);
        System.out.println("单行查询============");
        System.out.println(goods1);

        sql="select name from goods where id = ?";//sql语句写错也会报错
        Object o = goodsDAO.queryScalar(sql, 4);
        System.out.println("单行单列查询===========");
        System.out.println(o);
    }
    @Test//编写方法测试GoodsDAO对goods表的dml操作
    public void testDML(){
        GoodsDAO goodsDAO = new GoodsDAO();

        String sql1 = "insert into goods values (?,?,?)";//增
        String sql2 = "delete from goods where id = ?";//删
        String sql3 = "update goods set price = ? where id = ?";//改

        int i1 = goodsDAO.update(sql1, 5, "卷王心法", 8888);//增
        int i2 = goodsDAO.update(sql2, 3);//删
        int i3 = goodsDAO.update(sql3, 9999, 4);//改

        System.out.println(i1>0?"插入成功":"未成功");
        System.out.println(i2>0?"删除成功":"未成功");
        System.out.println(i3>0?"修改成功":"未成功");
    }
}
